java 如何读取 jar 中的文件资源和路径 您所在的位置:网站首页 java引用jar包 运行时改变jar包中的类 java 如何读取 jar 中的文件资源和路径

java 如何读取 jar 中的文件资源和路径

2024-07-16 11:02| 来源: 网络整理| 查看: 265

问题描述:项目需要将生成的文本存储在运行环境中,当我用相对路径 safe/certificate.pem存储时,出现了两种情况:

第一种:用idea启动项目 这时生成的文本会在编译后的 target.classes下创建对应目录,这时用百度上千篇一律的如下代码可以成功读取。问题的关键在于直接用idea启动的不是项目编译后的jar包 !

public String getCert() { java.net.URL fileURL = this.getClass().getResource("/safe/certificate.pem"); if (fileURL == null) { return "没有读取到证书!"; } File file = new File(fileURL.getPath()); long filelength = file.length(); byte[] filecontent = new byte[(int) filelength]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (IOException e) { e.printStackTrace(); } return new String(filecontent); }

请添加图片描述 第二种:用cmd 运行jar 包启动项目 通过jar运行的项目,会在jar包存储路径中生成jar同名文件,此时相对路径safe/certificate.pem生成的文本会在 jar 同名文件目录下创建对应目录,如下图蓝色框内容,这时通过上述的代码this.getClass().getResource(文件名)是获取不到文件的,会是null请添加图片描述 解决办法

得到 jar 文件路径 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); //path : file:/C:/Users/Mr.Tan/Documents/IT/work/HB/danger-backend/danger-client/cipher-client/target/cipher-client.jar!/BOOT-INF/classes!/ 解决可能存在的乱码 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); try { path = java.net.URLDecoder.decode(path, "UTF-8"); log.info("utf-8 path: " + path); } catch (java.io.UnsupportedEncodingException ex) { log.error(ex.getLocalizedMessage()); } // utf-8 path: file:/C:/Users/Mr.Tan/Documents/IT/work/HB/danger-backend/danger-client/cipher-client/target/cipher-client.jar!/BOOT-INF/classes!/ 获取绝对路径 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); log.info("path: " + path); try { path = java.net.URLDecoder.decode(path, "UTF-8"); log.info("utf-8 path: " + path); } catch (java.io.UnsupportedEncodingException ex) { log.error(ex.getLocalizedMessage()); } java.io.File jarFile = new java.io.File(path); log.info("jarFilepath: "+jarFile.getAbsolutePath() ); //jarFilepath: C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\file:\C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\cipher-client.jar!\BOOT-INF\classes! 获取上一级文件 java.io.File parent = jarFile.getParentFile(); log.info("parent: "+parent.getName()); log.info(parent.getAbsolutePath()); // parent: BOOT-INF // C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\file:\C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\cipher-client.jar!\BOOT-INF

参考代码 将生成的文件存到获取的绝对路径中,读取时从绝对路径读,将文件存在filet目录的上级目录, 在这里插入图片描述

/** * 获取jar包中的绝对路径 * * @return java.lang.String * @author luce * @date 2021/9/7 15:24 */ private String acquireJarPath() { //jar包编译后的相对路径 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile(); //消除乱码 try { path = java.net.URLDecoder.decode(path, "UTF-8"); } catch (java.io.UnsupportedEncodingException ex) { log.error(ex.getLocalizedMessage()); } //根据路径获取目标文件 java.io.File jarFile = new java.io.File(path); //获取文件的绝对路径 String jarFilepath = jarFile.getAbsolutePath(); //输出内容://jarFilepath: C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\file:\C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\cipher-client.jar!\BOOT-INF\classes! //我们需要得到 file 的上级目录 int end = jarFilepath.indexOf("file"); if (end > 0) { return jarFilepath.substring(0, end); //输出结果:C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\ } return jarFilepath + "\\"; } /** * 存证书文件 * * @param x509Certificate 证书 * @param certStoragePath 存储路径 -> C:\Users\Mr.Tan\Documents\IT\work\HB\cipher\target\safe\certificate.pem * @author luce * @date 2021/8/31 15:27 */ public static void createCert(X509Certificate x509Certificate, String certStoragePath) throws CertificateEncodingException { //先删除,预防在原有文本中续写 FileUtil.del(acquireJarPath() + "safe/certificate.pem"); //在指定目录生成证书文件 FileUtil.newFile(certStoragePath); //证书标题 FileUtil.appendUtf8String(BEGIN_CERT, certStoragePath); //分割线 FileUtil.appendUtf8String(System.getProperty("line.separator"), certStoragePath); //转Base64码的证书 FileUtil.appendUtf8String(Base64Encoder.encode(x509Certificate.getEncoded()), certStoragePath); //分割线 FileUtil.appendUtf8String(System.getProperty("line.separator"), certStoragePath); //尾文 FileUtil.appendUtf8String(END_CERT, certStoragePath); } /** * 读取证书 * * @return java.lang.String * @author luce * @date 2021/9/3 10:09 */ public String getCert() { File file = new File(acquireJarPath() + "safe/certificate.pem"); //读取的路径:C:\Users\Mr.Tan\Documents\IT\work\HB\danger-backend\danger-client\cipher-client\target\safe/certificate.pem long filelength = file.length(); byte[] filecontent = new byte[(int) filelength]; try { FileInputStream in = new FileInputStream(file); in.read(filecontent); in.close(); } catch (IOException e) { e.printStackTrace(); } return new String(filecontent); }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有